package com.kkb.controller;

import com.alibaba.fastjson.JSON;
import com.kkb.bean.Message;
import com.kkb.bean.Result;
import com.kkb.bean.TableUser;
import com.kkb.bean.User;
import com.kkb.mvc.ResponseBody;
import com.kkb.service.AdminService;
import com.kkb.utils.DateFormatUtil;
import com.kkb.utils.JSONUtil;
import com.kkb.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class AdminController {

    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean login = AdminService.login(username, password);
        // 根据结果，返回数据
        Message message = null;
        if (login) {
            HttpSession session = request.getSession();
            session.setAttribute("adminUserName",username);
            // 存入手机号
            UserUtil.setUserInfo(session,new User("18838438538"));
            // 登录成功
            message = new Message(0, "登录成功");
            // 登录时间 和 ip的更新
            Date date = new Date();
            String ip = request.getRemoteAddr();
            AdminService.updateLoginTimeAndIp(username,date,ip);
        } else{
            message = new Message(-1,"登录失败");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/admin/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        session.invalidate();
        Message message = new Message(0,"成功退出");
        return JSONUtil.toJSON(message);
    }

}
